package com.heima.controller;

import com.heima.annotation.OperationFill;
import com.heima.dto.DeptDto;
import com.heima.dto.pagedto.DeptPageDto;
import com.heima.entity.Dept;
import com.heima.result.Result;
import com.heima.service.DeptService;
import com.heima.vo.DeptNode;
import com.heima.vo.DeptVo;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dept")
@RequiredArgsConstructor
@Api(tags = "部门模块")
public class DeptController {
    private final DeptService deptService;

    /**
     * 添加部门
     * @param deptDto
     * @return
     */
    @PostMapping
    @OperationFill
    public Result add(@RequestBody DeptDto deptDto) {
        deptService.add(deptDto);
        return Result.success();
    }

    /**
     * 分页查询部门
     * @param pageDto
     * @return
     */
    @GetMapping("/list")
    @OperationFill
    public Result pageQuery(DeptPageDto pageDto) {
        List<DeptVo> deptVoList = deptService.pageQuery(pageDto);
        return Result.success(deptVoList);
    }

    /**
     * 根据 id 查询部门
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @OperationFill
    public Result queryById(@PathVariable Long id) {
        DeptVo deptVo = deptService.queryById(id);
        return Result.success(deptVo);
    }

    /**
     * 更新部门
     * @param deptDto
     * @return
     */
    @PutMapping
    @OperationFill
    public Result update(@RequestBody DeptDto deptDto) {
        Result result = deptService.update(deptDto);
        return result;
    }

    /**
     * 部门树状展示
     * @return
     */
    @GetMapping("/treeselect")
    public Result treeSelect() {
        List<DeptNode> deptNode = deptService.treeSelect();
        return Result.success(deptNode);
    }

    /**
     * 获取除自身外的部门
     * @param id
     * @return
     */
    @GetMapping("/list/exclude/{id}")
    public Result exclude(@PathVariable Integer id) {
        List<Dept> dept =  deptService.exclude(id);
        return Result.success(dept);
    }


    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Long id) {
        Result result = deptService.deleteDept(id);
        return result;
    }

}
